<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="统计日期">
        <el-date-picker v-model="queryParams.date" value-format="yyyy-MM" type="month" placeholder="请选择统计日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="项目名称">
        <el-select multiple v-model="queryParams.projectName" placeholder="请选择项目名称">
          <el-option v-for="(item, index) in projectNameOptions" :label="item.label" :value="item.value"
            :key="index"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="项目状态">
        <el-select multiple v-model="queryParams.status" placeholder="请选择项目状态" clearable>
          <el-option v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="序号" align="center" width="50" prop="index" fiexd="left" />
      <el-table-column label="所属单位" align="center" prop="companyName" width="120" fixed="left" />
      <el-table-column label="项目名称" align="center" prop="projectName" width="120" fiexd="left" />
      <el-table-column label="总计" align="center" prop="total" width="120" fiexd="left" />
      <el-table-column v-for="(item, index) in columns" :label="item" align="center" prop="item" :key="index" width="80">
        <template slot-scope="scope">
          <span :class="{ red: scope.row[item] == 0 }">{{ scope.row[item] }}</span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import { mapState } from "vuex";
export default {
  name: "educateStat",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      projectId: this.$store.state.app.selectData,
      showSearch: true,
      projectNameOptions: [],
      queryParams: {
        date: "",
        projectName: [],
        status: [],
      },
      columns: [],
      loading: false,
      list: [],
    }
  },
  mounted() {
    const date1 = this.parseTime(new Date(), '{y}-{m}');
    this.queryParams.date = date1;
    // this.queryParams.status = []
    // this.dict.type.sys_normal_disable.forEach((item) => {
    //   this.queryParams.status.push(item.value);
    // })
    this.getProjectNameOptions();
    if (this.projectId) {
      this.getList();
    }
  },
  watch: {
    app: {
      handler(newVal, oldVal) {
        this.projectId = newVal.selectData;
        this.getList();
      },
      deep: true,
    },
  },
  computed: {
    ...mapState(["app"]),
  },
  methods: {
    async getProjectNameOptions() {
      this.projectNameOptions = [];
      const obj1 = { value: '1', label: '项目名称1' }
      const obj2 = { value: '2', label: '项目名称2' }
      this.projectNameOptions.push(obj1, obj2);
      // this.queryParams.projectName = []
      // this.projectNameOptions.forEach(item => {
      //   this.queryParams.projectName.push(item.value);
      // })
    },
    async getList() {
      this.loading = true;
      const res = [{
        id: "1",
        companyName: "公司1",
        projectName: "项目1",
        total: 10086,
        data: [
          { label: "1号", num: 1 },
          { label: "2号", num: 2 },
          { label: "3号", num: 0 },
          { label: "4号", num: 4 },
        ]
      }]
      this.generateColumns(res[0]);
      this.list = this.generateData(res);
      this.total = 1;
      this.loading = false;
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    generateColumns(obj) {
      this.columns = []
      obj.data.forEach(item => {
        this.columns.push(item.label)
      })
    },
    generateData(res) {
      res.forEach((item, index) => {
        item.index = index + 1
        const data = item.data;
        data.forEach(dataItem => {
          item[dataItem.label] = dataItem.num;
        })
      })
      return res;
    }
  },
}
</script>
<style lang="scss" scoped>
.red {
  color: red;
}
</style>
